package com.wangzhixuan.filter;

import com.wangzhixuan.constant.SystemConstant;
import com.wangzhixuan.db.bean.Customer;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangzhixuan.service.CustomerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * Customer Admin filtering
 *
 * @author Herbert
 *
 */
@Component("loginFilter")
public class LoginFilter implements Filter {

    @Autowired
    private CustomerService customerService;
    protected final Logger logger = Logger.getLogger(this.getClass());

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest,
            ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Customer currentCustomer = (Customer) request.getSession().getAttribute(
                SystemConstant.SESSION_CUSTOMER_KEY);
        if(currentCustomer==null){
            currentCustomer=customerService.getCustomerById(1);
            HttpSession session = request.getSession();
            currentCustomer.setPassword("");
            session.setAttribute(SystemConstant.SESSION_CUSTOMER_KEY, currentCustomer);
        }
        String url=request.getRequestURI();
        boolean ifLoginUrl=false;
        if(url.endsWith("login")||url.endsWith("code") || url.endsWith("/wechat/portal") || url.endsWith("/pay/wxNotify")){
            ifLoginUrl=true;
        }
        if (currentCustomer == null && !ifLoginUrl&&!chcekStaticFiles(url)) {
            String path = request.getContextPath();
            String basePath = request.getScheme() + "://"
                    + request.getServerName() + ":" + request.getServerPort()
                    + path;
            response.sendRedirect(basePath + "/login");
        } else {
            chain.doFilter(request, response);
        }
    }

    public boolean chcekStaticFiles(String url){
        boolean ifStatic=false;
        if(url.endsWith(".js")){
            ifStatic=true;
        }
        if(url.endsWith(".css")){
            ifStatic=true;
        }
        if(url.endsWith(".png")){
            ifStatic=true;
        }
        if(url.endsWith(".jpg")){
            ifStatic=true;
        }
        if(url.endsWith(".gif")){
            ifStatic=true;
        }
        if(url.endsWith(".txt")){
            ifStatic=true;
        }
        return ifStatic;
    }
    @Override
    public void destroy() {
    }

}
